#include "pch.h"
#include "UIWndManager.h"
#include "GaojieDriveApp.h"
#include "UITrayIconWnd.h"
#include "UISettingsDlg.h"
#include "UIAboutDlg.h"

CUIWndManager::CUIWndManager()
	: m_hIcon(NULL)
{
	AssertInUIThread();
}

CUIWndManager::~CUIWndManager()
{
	AssertInUIThread();
}

bool CUIWndManager::Init()
{
	AssertInUIThread();

	auto pWnd = new CUITrayIconWnd;
	m_hWnds[TrayIcon] = pWnd->Create(NULL);
	return true;
}

void CUIWndManager::Exit()
{
	AssertInUIThread();

	DestroyWnds();
}

void CUIWndManager::ShowSettings()
{
	AssertInUIThread();

	HWND& hWnd = m_hWnds[Settings];

	if (IsWindow(hWnd))
	{
		SetForegroundWindow(hWnd);
	}
	else
	{
		auto pDlg = new CUISettingsDlg(false);
		hWnd = pDlg->Create(NULL);
		pDlg->ShowWindow(SW_SHOWNORMAL);
	}
}

void CUIWndManager::ShowAbout()
{
	AssertInUIThread();

	HWND& hWnd = m_hWnds[About];

	if (IsWindow(hWnd))
	{
		SetForegroundWindow(hWnd);
	}
	else
	{
		auto pDlg = new CUIAboutDlg;
		hWnd = pDlg->Create(NULL);
		pDlg->ShowWindow(SW_SHOWNORMAL);
	}
}

void CUIWndManager::DestroyWnds()
{
	AssertInUIThread();

	for (HWND hWnd : m_hWnds)
	{
		if (IsWindow(hWnd))
			DestroyWindow(hWnd);
	}
}

HICON CUIWndManager::GetMainIcon()
{
	AssertInUIThread();

	if (m_hIcon == NULL)
		m_hIcon = LoadIcon(GetApp()->GetInstance(), MAKEINTRESOURCE(IDI_GAOJIEDRIVE));

	return m_hIcon;
}
